<?php

namespace App\Http\Controllers\Admin;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoriesController extends Controller
{
    public function fisrtIndex(Request $request)
    {
        $list = Category::where('deleted',0)->where('parent_id',0)->get();

        $items = [
            [
                'id' => 0,
                'text' => '请选择'
            ]
        ];

        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $items[] = ['id'=>$v->id,'text'=>$v->title];
            }
        }
        return $items;
    }

    public function secondIndex(Request $request)
    {
        $parent_id = $request->get('q');

        $items = [
            [
                'id' => 0,
                'text' => '请选择'
            ]
        ];

        if($parent_id)
        {
            $list = Category::where('deleted',0)->where('parent_id',$parent_id)->get();
            if(!empty($list))
            {
                foreach ($list as $v)
                {
                    $items[] = ['id'=>$v->id,'text'=>$v->title];
                }
            }
        }

        return $items;
    }
}
